local suhui = fk.CreateSkill {
  name = "th_suhui",
}

suhui:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(suhui.name) then return end
    local ids = {}
    local room = player.room
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        if move.moveReason == fk.ReasonDiscard and move.from and move.from ~= player then
          for _, info in ipairs(move.moveInfo) do
            if (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) and
            Fk:getCardById(info.cardId).color == Card.Black and
            room:getCardArea(info.cardId) == Card.DiscardPile then
              table.insertIfNeed(ids, info.cardId)
            end
          end
        elseif move.moveReason == fk.ReasonJudge and move.skillName == "" then
          local judge_event = room.logic:getCurrentEvent():findParent(GameEvent.Judge)
          if judge_event and judge_event.data.who ~= player then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.Processing and Fk:getCardById(info.cardId).color == Card.Black and
              room:getCardArea(info.cardId) == Card.DiscardPile then
                table.insertIfNeed(ids, info.cardId)
              end
            end
          end
        end
      end
    end
    ids = room.logic:moveCardsHoldingAreaCheck(ids)
    if #ids > 0 then
      local max_num = math.max((player.maxHp - player:getMark("@th_suhui-round")), 0)
      if max_num <= 0 then return end
      event:setCostData(self, { cards = ids, num = max_num })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local ids, num = dat.cards, dat.num
    num = math.min(num, #ids)
    if #ids > 1 then
      local cards, _ = room:askToChooseCardsAndChoice(player, {
        min_num = 1,
        max_num = num,
        cards = ids,
        choices = {"OK"},
        skill_name = suhui.name,
        prompt = "#th_suhui-choose:::" .. num
      })
      if #cards > 0 then
        ids = cards
      end
    end
    room:moveCardTo(ids, Card.PlayerHand, player, fk.ReasonJustMove, suhui.name)
    room:addPlayerMark(player, "@th_suhui-round", #ids)
  end,
})

Fk:loadTranslationTable{
  ["th_suhui"] = "溯洄",
  [":th_suhui"] = "当其他角色的牌因弃置或判定而置入弃牌堆后，你可以获得其中任意张黑色牌（每轮以此法获得牌数至多为你的体力上限）。",

  ["#th_suhui-choose"] = "溯洄：你可以获得其中至多%arg张牌。",

  ["@th_suhui-round"] = "溯洄",
}

return suhui